from django.http.response import JsonResponse
from django.core.serializers.json import DjangoJSONEncoder


class ResponseUtil(object):
    @staticmethod
    def res_data(code, msg, data, success, total=None):
        return JsonResponse({
            "code": code,  # http转态嘛
            "msg": msg,  # token失效
            "data": data,  #
            "success": success,  # True False
            "total": total  # 没有分页 page pagesize 总数 : total
        })

    @staticmethod
    def res_page_data():
        pass


class JsonSuccessResponse(JsonResponse):
    def __init__(self, data, msg='success', total=0, success=True, encoder=DjangoJSONEncoder, safe=True,
                 json_dumps_params=None, **kwargs):
        std_data = {
            "code": 200,
            "data": data,
            "msg": msg,
            "total": total,
            "success": success,
        }
        super().__init__(std_data, encoder=encoder, safe=safe,
                         json_dumps_params=json_dumps_params, **kwargs)


class JsonErrorResponse(JsonResponse):
    def __init__(self, data=None, code=500, msg='error', success=False, encoder=DjangoJSONEncoder, safe=True,
                 json_dumps_params=None, **kwargs):
        std_data = {
            "code": code,
            "data": data,
            "msg": msg,
            "success": success,
        }
        super().__init__(std_data, encoder=encoder, safe=safe,
                         json_dumps_params=json_dumps_params, **kwargs)
